<?php

include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/app/inc.php");

//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	
	cgi::both($g_cgival["id"], "id", "");
	$g_cgival["id"] = intval($g_cgival["id"]);
	
	cgi::both($g_cgival["link"], "link", "");
	$g_cgival["link"] = string::un_html($g_cgival["link"]);
	
	cgi::both($g_cgival["content"], "content", "");
	$g_cgival["content"] = string::un_html($g_cgival["content"]);
	
	cgi::both($g_cgival["pic"], "pic", "");
	$g_cgival["pic"] = string::un_html($g_cgival["pic"]);
	
	cgi::both($g_cgival["type"], "type", "add");
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	$pic = $g_cgival["pic"];
	$id = $g_cgival["id"];
	$link = $g_cgival["link"];
	$content = $g_cgival["content"];
	
	
	$db_app_data_detail = new db_app_data_detail();
	//登录相关
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		$g_show["is_logined"] = true;
		$g_show["user_info"] = $g_show["current_user_info"] = $user_detail_info;
	}
	else
	{
		$url = "/user/login.php?url={$_SERVER["REQUEST_URI"]}";
		header("Location:$url");
	}	
	
	
	if($g_cgival["type"] == "add")
	{
		if($pic)
		{
			
			$res = get_image::get_image_size_local(file_get_contents($_SERVER['DOCUMENT_ROOT'] .$pic));
			
		
			$srcW = $res["width"]; 
			$srcH = $res["height"]; 
			$pic_width = PUBU_WIDTH;
			
			$pic_height= intval($srcH/($srcW/$pic_width));
			
		}
		if($id)
		{
			$res = $db_app_data_detail->admin_update_by_id($id,"",$pic,$link,$content,$pic_width,$pic_height);
		}
		
		else
		{
			$res =$db_app_data_detail->admin_insert_one($pic,$link,$content,$uid,$pic_width,$pic_height,$user_nickname);
		}
		
	}
	elseif($g_cgival["type"] == "del")
	{
		$res =$db_app_data_detail->admin_update_by_id($id,0);
	}
	
		
	
	if(!$res)
	{
		throw new Exception("操作失败！");
	}
}

try {
	$g_show['error'] = 0;
	$g_show['errmsg'] = "";
	check_cgi_pro();
	get_data();
	
	$g_show["hide_shadow"] = true;
	
} catch (Exception $e) {
	$g_show['error'] = 1;
	$g_show['errmsg'] = $e->getMessage();
}

show_pro();


exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	echo json_encode($g_show);
	
}

